package org.jeecg.modules.tls.csmg.csnote.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.checkerframework.checker.units.qual.C;
import org.jeecg.modules.tls.csmg.csnote.entity.CsNote;
import org.jeecg.modules.tls.csmg.csnote.mapper.CsNoteMapper;
import org.jeecg.modules.tls.csmg.csnote.service.ICsNoteService;
import org.jeecg.modules.tls.csmg.csnotelike.entity.CsNoteLike;
import org.jeecg.modules.tls.csmg.csnotelike.mapper.CsNoteLikeMapper;
import org.jeecg.modules.tls.csmg.viewmodel.note.PrivateNoteVM;
import org.jeecg.modules.tls.csmg.viewmodel.note.PublicNoteVM;
import org.jeecg.modules.user.entity.SysUserDemo;
import org.jeecg.modules.user.mapper.SysUserDemoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: cs_note
 * @Author: jeecg-boot
 * @Date:   2022-08-05
 * @Version: V1.0
 */
@Service
public class CsNoteServiceImpl extends ServiceImpl<CsNoteMapper, CsNote> implements ICsNoteService {
    @Autowired
    CsNoteMapper csNoteMapper;
    @Autowired
    SysUserDemoMapper sysUserDemoMapper;
    @Autowired
    CsNoteLikeMapper csNoteLikeMapper;
    @Override
    public Boolean updateNote(String id, String content,Integer hasPublic) {
        CsNote csNote1 = csNoteMapper.selectById(id);
        CsNote csNote = new CsNote();
        csNote.setId(id);
        csNote.setContent(content);
        if((csNote1.getHasPublic()>=0&&hasPublic==-1)||(csNote1.getHasPublic()==-1&&hasPublic==0)){
            csNote.setHasPublic(hasPublic);
        }

        return csNoteMapper.updateById(csNote)>0;
    }

    @Override
    public List<PrivateNoteVM> getPrivateNoteList(String sectionId, String userId) {
        QueryWrapper<CsNote> csNoteQueryWrapper = new QueryWrapper<>();
        csNoteQueryWrapper.eq("section_id",sectionId).eq("user_id",userId).orderByDesc("create_time");
        List<CsNote> csNoteList = csNoteMapper.selectList(csNoteQueryWrapper);
        List<PrivateNoteVM> privateNoteVMList =  csNoteList.stream().map(i->{
            PrivateNoteVM privateNoteVM = new PrivateNoteVM();
            SysUserDemo sysUserDemo = sysUserDemoMapper.selectById(i.getUserId());
            privateNoteVM.setAvatar(sysUserDemo.getAvatar());
            privateNoteVM.setUserName(sysUserDemo.getRealname());
            privateNoteVM.setId(i.getId());
            privateNoteVM.setContent(i.getContent());
            privateNoteVM.setUserId(i.getUserId());
            privateNoteVM.setCreateTime(i.getCreateTime());
            privateNoteVM.setHasPublic(i.getHasPublic());
            privateNoteVM.setPlaybackTime(i.getPlaybackTime());
            privateNoteVM.setSectionId(i.getSectionId());
            return privateNoteVM;
        }).collect(Collectors.toList());
        return privateNoteVMList;
    }

    @Override
    public IPage<PublicNoteVM> getPublicNotePage(String sectionId, String userId,Integer pageIndex,Integer pageSize) {
        QueryWrapper<CsNote> csNoteQueryWrapper = new QueryWrapper<>();
        csNoteQueryWrapper.eq("section_id",sectionId).ge("has_public",0).orderByDesc("has_public");
        IPage<CsNote> page = new Page<>(pageIndex,pageSize);
        IPage<CsNote> csNoteIPage = csNoteMapper.selectPage(page,csNoteQueryWrapper);

        List<PublicNoteVM> publicNoteVMList = csNoteIPage.getRecords().stream().map(i->{
            PublicNoteVM publicNoteVM = new PublicNoteVM();
            SysUserDemo sysUserDemo = sysUserDemoMapper.selectById(i.getUserId());
            //是否点赞
            QueryWrapper<CsNoteLike> csNoteLikeQueryWrapper = new QueryWrapper<>();
            csNoteLikeQueryWrapper.eq("user_id",userId).eq("note_id",i.getId());
            Boolean hasLike = csNoteLikeMapper.exists(csNoteLikeQueryWrapper);

            publicNoteVM.setHasPublic(i.getHasPublic());
            publicNoteVM.setContent(i.getContent());
            publicNoteVM.setAvatar(sysUserDemo.getAvatar());
            publicNoteVM.setHasLike(hasLike);
            publicNoteVM.setId(i.getId());
            publicNoteVM.setLikeNum(i.getHasPublic());
            publicNoteVM.setCreateTime(i.getCreateTime());
            publicNoteVM.setPlaybackTime(i.getPlaybackTime());
            publicNoteVM.setSectionId(i.getSectionId());
            publicNoteVM.setUserId(i.getUserId());
            publicNoteVM.setUserName(sysUserDemo.getRealname());
            return publicNoteVM;
        }).collect(Collectors.toList());
        IPage<PublicNoteVM> publicNoteVMIPage = new Page<>();
        publicNoteVMIPage.setPages(csNoteIPage.getPages());
        publicNoteVMIPage.setRecords(publicNoteVMList);
        publicNoteVMIPage.setTotal(csNoteIPage.getTotal());
        publicNoteVMIPage.setSize(csNoteIPage.getSize());
        publicNoteVMIPage.setCurrent(csNoteIPage.getCurrent());

        return publicNoteVMIPage;
    }
}
